package com.topscomm.main.config;

import java.util.Map;

import com.topscomm.pub.util.SecurityUtil;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.topscomm.core.AbstractFileStoreService;
import com.topscomm.core.FileStoreTypeEnum;
import com.topscomm.core.FileTypeEnum;
import com.topscomm.core.IFileStoreService;
import com.topscomm.core.factory.FileStoreFactoryService;
import com.topscomm.core.factory.ftp.FtpFileStoreService;
import com.topscomm.core.factory.minio.MinioFileStoreService;
import com.topscomm.core.sign.SimpleFileManager;
import com.topscomm.file.FileProperties;
import com.topscomm.file.FtpConfig;
import com.topscomm.file.MinioConfig;
import com.topscomm.file.convert.IFileConvertService;
import com.topscomm.file.convert.aspose.AsposeFileConvertService;
import com.topscomm.file.convert.factory.FileConvertFactoryService;
import com.topscomm.file.convert.ffmpeg.FfmpegFileConvertService;

/**
 * 文件存储服务类
 *
 * @description:
 * @author: donghang
 * @date: 2021年3月13日下午11:35:52
 * @modify:
 */
@Configuration
public class FileServiceConfig {

	/**
	 * 文件配置信息
	 *
	 * @description:
	 * @return
	 * @author: donghang
	 * @date: 2021年3月16日下午2:03:40
	 * @modify:
	 */
	@Bean
	@ConfigurationProperties(prefix = "file")
	public FileProperties fileProperties() {
		return new FileProperties();
	}

	/**
	 * @description:
	 * @param fileProperties
	 * @return
	 * @author: donghang
	 * @date: 2021年3月16日下午2:03:56
	 * @modify:
	 */
	@Bean
	public SimpleFileManager simpleFileManager(FileProperties fileProperties) {
		;
		return new SimpleFileManager(SecurityUtil.decryptDes(fileProperties.getHmacFilePassword(), fileProperties.getFtp().getDecryptKey()), fileProperties.getFileEndpoint());
	}

	/**
	 * ftp服务类
	 *
	 * @description:
	 * @param fileProperties
	 * @return
	 * @author: donghang
	 * @date: 2021年3月13日下午11:36:11
	 * @modify:
	 */
	@Bean(name = "ftpFileService")
	public AbstractFileStoreService ftpFileService(FileProperties fileProperties) {
		FtpConfig ftpConfig = fileProperties.getFtp();
		AbstractFileStoreService ftpStoreService = new FtpFileStoreService(ftpConfig);
		return ftpStoreService;
	}

	/**
	 * minio服务类
	 *
	 * @description:
	 * @param fileProperties
	 * @return
	 * @author: donghang
	 * @date: 2021年3月13日下午11:36:27
	 * @modify:
	 */
	@Bean(name = "minioFileService")
	public AbstractFileStoreService minioFileService(FileProperties fileProperties) {
		MinioConfig minioConfig = fileProperties.getMinio();
		AbstractFileStoreService minioFileService = new MinioFileStoreService(minioConfig);
		return minioFileService;
	}

	/**
	 * 文件服务工厂
	 *
	 * @description:
	 * @param applicationContext
	 * @return
	 * @author: donghang
	 * @date: 2021年3月13日下午11:36:56
	 * @modify:
	 */
	@Bean(name = "fileStoreFactoryService")
	@Primary
	public FileStoreFactoryService fileStoreFactoryService(ApplicationContext applicationContext) {
		IFileStoreService minioFileService = (IFileStoreService) applicationContext.getBean("minioFileService");
		FileStoreFactoryService fileStoreFactoryService = new FileStoreFactoryService(minioFileService);
		Map<String, AbstractFileStoreService> storeServiceMap = applicationContext
				.getBeansOfType(AbstractFileStoreService.class);
		storeServiceMap.keySet().stream().forEach(key -> {
			IFileStoreService fileStoreService = storeServiceMap.get(key);
			fileStoreFactoryService.addStoreService(FileStoreTypeEnum.protocol(fileStoreService.getStoreProtocol()),
					fileStoreService);
		});
		return fileStoreFactoryService;
	}

	/**
	 * apose文件转换服务
	 *
	 * @description:
	 * @return
	 * @author: donghang
	 * @date: 2021年3月15日下午7:17:20
	 * @modify:
	 */
	@Bean(name = "aposeFileConvertService")
	public IFileConvertService aposeFileConvertService() {
		IFileConvertService fileConvertService = new AsposeFileConvertService();
		return fileConvertService;
	}

	/**
	 * ffmpeg文件转换服务
	 *
	 * @description:
	 * @return
	 * @author: donghang
	 * @date: 2021年3月15日下午7:17:20
	 * @modify:
	 */
	@Bean(name = "ffmpegFileConvertService")
	public IFileConvertService ffmpegFileConvertService() {
		IFileConvertService fileConvertService = new FfmpegFileConvertService();
		return fileConvertService;
	}

	/**
	 * 文件转换服务工厂
	 *
	 * @description:
	 * @param applicationContext
	 * @return
	 * @author: donghang
	 * @date: 2021年3月13日下午11:36:56
	 * @modify:
	 */
	@Bean(name = "fileConvertFactoryService")
	@Primary
	public FileConvertFactoryService fileConvertFactoryService(ApplicationContext applicationContext) {
		IFileConvertService aposeFileConvertService = (IFileConvertService) applicationContext
				.getBean("aposeFileConvertService");
		IFileConvertService ffmpegFileConvertService = (IFileConvertService) applicationContext
				.getBean("ffmpegFileConvertService");
		FileConvertFactoryService fileConvertFactoryService = new FileConvertFactoryService();
		fileConvertFactoryService.addConvertService(FileTypeEnum.AUDIOVIDEO, ffmpegFileConvertService);
		fileConvertFactoryService.addConvertService(FileTypeEnum.OFFICE, aposeFileConvertService);
		return fileConvertFactoryService;
	}
}
